﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.iss.inventory.Entity;
using com.iss;
using System.Runtime.Serialization;
using System.Xml;


namespace StationeryInv
{
    public partial class DeliveryOrder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Populate PO Number Textbox automatically from latest record. 
            //User can also type in PO number, to search for any existing PO.
            if (!IsPostBack)
            {
                string currentPONumber = Convert.ToString(Session["tbPONumber"]);
                tbPONumber.Text = currentPONumber;

                //Populate DO Number Automatically
                InvManagementEntities invent = new InvManagementEntities();

                int value = int.Parse(invent.DeliveryOrders
                               .OrderByDescending(p => p.DeliveryID)
                               .Select(r => r.DeliveryID)
                               .First().ToString());
                // int a = value + 1;
                lblDONumber.Text = Convert.ToString(value);

                //Populate Today's date in textbox
                DateTime dateonly = DateTime.Now.Date;
                tbDeliveryDate.Text = dateonly.ToString("dd-MM-yyyy");
            }

            



        }

       

       //Gets Purchase Order Record and Populates the Gridview
        protected void btnGetPO_Click(object sender, EventArgs e)
        {

            //Populate the Supplier Name Textbox
            InvManagementEntities invent = new InvManagementEntities();
            int purchaseID = Convert.ToInt32(tbPONumber.Text);

            var supplierName = (from p in invent.PurchaseOrders
                                join s in invent.Suppliers on p.SupplierID equals s.SupplierID
                                where p.PurchaseID == purchaseID

                                select s.Name).First();

            tbsupplierName.Text = supplierName.ToString();


            //Populate Gridview Delivery Orders
            List<object> list = new List<object>();

            int purchaseID1 = Convert.ToInt32(tbPONumber.Text);
            var t = from d in invent.DeliveryOrders
                    join p in invent.PurchaseOrders on d.PurchaseID equals p.PurchaseID
                    join o in invent.PurchaseOrderDetails on p.PurchaseID equals o.PurchaseID
                    where p.PurchaseID == purchaseID1

                    select new { o.ItemID, o.Description, o.Qty };

            list = t.ToList<object>();
            GviewDO.DataSource = list;
            GviewDO.DataBind();



        }

        //Inserts values of current gridview into database
        protected void btnGenerateDO_Click(object sender, EventArgs e)
        {



            //Insert values of grid into database (Delivery Orders)
            
            using (InvManagementEntities invent = new InvManagementEntities())
            {
                int purchaseID3 = Convert.ToInt32(tbPONumber.Text);
                var addtoDO = invent.DeliveryOrders.FirstOrDefault(m => m.PurchaseID == purchaseID3);

                if (addtoDO != null)
                {

                    addtoDO.PurchaseID = Convert.ToInt32(tbPONumber.Text);

                    var signedById = (from f in invent.Employees
                                      where f.Name == ddlSignedBy.SelectedValue

                                      select f.EmployeeID).First();

                    addtoDO.SignedbyID = Convert.ToInt32(signedById);
                    addtoDO.SupplierBillNo = Convert.ToInt32(tbSupplierBillNo.Text);
                    addtoDO.DateDelivered = Convert.ToDateTime(tbDeliveryDate.Text);
                   // invent.AddToDeliveryOrders(addtoDO);

                    invent.SaveChanges();

                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Delivery Order Saved Successfully')", true);

                    
                }
            }



            //Insert values of grid into database (Purchase Orders - Remarks Column, and update Stationery Items Qty)
            using (InvManagementEntities invent = new InvManagementEntities())
            {
               
                int purchaseID2 = Convert.ToInt32(tbPONumber.Text);

               
                
                var addtoPO = invent.PurchaseOrders.First(m => m.PurchaseID ==purchaseID2);
              

                                     
                if (addtoPO != null)
                {

                    List<string> remarksstring = new List<string>();
                  //  List<int> qtyleft = new List<int>();
                    string s = null;
                    foreach (GridViewRow gvrow in GviewDO.Rows)
                    {
                        //Enter remarks into database
                        TextBox tbremarks1 = gvrow.FindControl("Remarks") as TextBox;
                        s = Convert.ToString(tbremarks1.Text);
                        remarksstring.Add(s);


                     int itemID =   Convert.ToInt32(gvrow.Cells[0].Text);

                     //int qtyleft1 =  (int) (from si in invent.StationeryItems

                     //                     where si.ItemID == itemID
                     //                     select si.QtyLeft).First();


                        int q = Convert.ToInt32(gvrow.Cells[2].Text);

                       var addtoSI = invent.StationeryItems.First(m => m.ItemID == itemID);
                       addtoSI.QtyLeft += q;

                       invent.SaveChanges();
                        //qtyleft.Add(q);
                    }
                    string remark = null;
                    foreach (string re in remarksstring)
                    {
                        remark += re + " ";
                    }

                    addtoPO.Remarks = remark;

                 

                    invent.SaveChanges();
                }
            }
        }

       

       
    }
}